package studio.raptor.hub.in.server;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import org.apache.http.client.HttpClient;
import studio.raptor.hub.in.ServerProxy;
import studio.raptor.hub.in.server.AccountProxy.Builder;

/**
 * Created by liuyong8 on 2017/7/27.
 */
public class CustomerProxy extends ServerProxy {

  //    {
  //        "qryCustomerDetailReq": {
  //        "custId": "10001"
  //    }
  //    }
  private static final String REQ_MESSAGE = "{\"qryCustomerDetailReq\": {\"custId\": \"%s\"}}";

  private CustomerProxy(Builder builder) {
    super(builder.getHttpClient());
  }

  public static class Builder {
    private HttpClient httpClient;

    public CustomerProxy build() {
      return new CustomerProxy(this);
    }

    public Builder httpClient(HttpClient httpclient) {
      this.httpClient = httpclient;
      return this;
    }

    public HttpClient getHttpClient() {
      return httpClient;
    }

  }

  @Override
  protected String handleResponse(String response) {
    DocumentContext sc = JsonPath.parse(response);
    Object object = sc.read("$.resultObject");
    String result =
        JSON.toJSONString(object, SerializerFeature.PrettyFormat,
            SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat);
    if (result.startsWith("{")) {
      result = result.substring(1, result.length() - 1);
    }
    return result;
  }

  @Override
  protected String getMessage(Long... params) {
    return String.format(REQ_MESSAGE, params);
  }
}
